#include "MySqlTableModel.h"
#include <QColor>
#include <QFont>
#include <QDebug>


MySqlTableModel::MySqlTableModel(QObject * parent, QSqlDatabase db):QSqlTableModel(parent, db)
{
}

MySqlTableModel::~MySqlTableModel()
{
}

QVariant MySqlTableModel::data(const QModelIndex &idx, int role) const
{
  QVariant value = QSqlTableModel::data(idx, role);
  if(Qt::TextAlignmentRole == role)
  {
      value = int(Qt::AlignCenter | Qt::AlignHCenter);
      return value;
  }
  if(Qt::ForegroundRole == role)
  {

  }

  if(Qt::FontRole == role)
  {

  }

  return value;
}

bool MySqlTableModel::recordContains(QSqlRecord *record, QVector<QString> name)
{
    if(record->isEmpty())
    {
       qDebug()<<"record is empty";
       return false;
    }

    for(int i = 0; i < name.size(); i++)
    {
        if(record->contains(name.at(i)) == false)
        {
            qDebug()<<"record does not have this name field";
            return false;
        }
    }

    return true;
}

bool MySqlTableModel::recordSetValue(QSqlRecord *record, QVector<QString> name, QVector<QVariant> value)
{

    if(recordContains(record, name) == false)
    {
        return false;
    }

    if(name.size() != value.size())
    {
        qDebug()<<"name size does not match value size";
        return false;
    }

    for(int i = 0; i < name.size(); i++)
    {
        record->setValue(name.at(i), value.at(i));
    }

    return true;
}

//Qt::ItemFlags MySqlTableModel::flags(const QModelIndex &index) const
//{
//    if(!index.isValid())
//        return Qt::NoItemFlags;

//    Qt::ItemFlags flag = QAbstractItemModel::flags(index);

//  //  flag |= Qt::ItemIsEditable; // 设置单元格可编辑，没有此句单元格将无法编辑
//    return flag;
//}
